import logging

from selenium.webdriver.chrome import webdriver
from selenium.webdriver.common.by import By

from common.doLog import DoLog
from common.get_element import GetElement

dolog = DoLog().set_get_logger(logging.INFO)
wd = webdriver.WebDriver()
ge = GetElement()

class SearchPage():
    # 输入文本
    content = (By.NAME, 'keywords')
    # 搜索按钮
    searchButton = (By.NAME, 'imageField')
    # 商品按钮
    product = (By.XPATH, '//*[@id="compareForm"]/div/div[1]/a/img')

    def searchPage(self, txt):
        wd.get('http://192.168.17.130:80')
        # find_element 需要两个参数，* 代表不定长元组
        # self.wd.find_element(*self.content).clear()
        # self.wd.find_element(*self.content).send_keys(txt)
        # self.wd.find_element(*self.searchButton).click()
        # self.wd.find_element(*self.product).click()
        try:
            ge.getElement(wd, *self.content).clear()
            ge.getElement(wd, *self.content).send_keys(txt)
            ge.getElement(wd, *self.searchButton).click()
            ge.getElement(wd, *self.product).click()
        except Exception as e:
            dolog.error(f"发生异常了，异常信息是{e}")
            print(f"{e}")
        else:
            dolog.info(f"运行成功")
        finally:
            dolog.info("运行完毕")


if __name__ == '__main__':
    search = SearchPage()
    search.searchPage('钻石')
